package com.wiscamp.ninechapters.common.core;

public class CodeHelper {

    private CodeHelper() {
    }

    static final char[] digits62 = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
            'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
            'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

    static final char[] digits36 = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
            'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

    private static String toRadixString(long number, char[] digits) {
        var builder = new StringBuilder();
        while (true) {
            int remainder = (int) (number % digits.length);
            builder.append(digits[remainder]);
            number = number / digits.length;
            if (number == 0) {
                break;
            }
        }
        return builder.reverse().toString();
    }

    /**
     * 十进制数字转62进制字符串
     *
     * @param number
     * @return
     */
    public static String to62RadixString(long number) {
        return toRadixString(number, digits62);
    }

    /**
     * 十进制数字转36进制字符串
     *
     * @param number
     * @return
     */
    public static String to36RadixString(long number) {
        return toRadixString(number, digits36);
    }

    /**
     * 当前时间转62进制字符串
     *
     * @return
     */
    public static String get62RadixDateTime() {
        long datetime = System.currentTimeMillis();
        return to62RadixString(datetime);
    }

    /**
     * 当前时间转36进制字符串
     *
     * @return
     */
    public static String get36RadixDateTime() {
        long datetime = System.currentTimeMillis();
        return to36RadixString(datetime);
    }
}
